﻿using Mapster;
using SqlSugar;
using Zhp.Common.Helper;
using Zhp.Common.WebApp;
using Zhp.Entity.Workflow;
using Zhp.IService.Workflow.Example;
using Zhp.Model.Workflow;
using Zhp.Service.Base;
using Zhp.SqlSugar.Repository;
using Zhp.Workflow.Models;
using Zhp.Workflow.Services;

namespace Zhp.Service.Workflow.Example
{
    public class WorkflowExampleService : BaseService<WorkflowExample>, IWorkflowExampleService
    {
        private readonly IWorkflowService _workflowService;
        public WorkflowExampleService(IBaseRepository<WorkflowExample> rep, IWorkflowService workflowService, ICurrentUser currentUser) 
        {
            _rep = rep;
            _workflowService = workflowService;
            _currentUser = currentUser;
        }

        public async Task<List<WorkflowExampleModel>> Query(WorkflowExampleQuery query)
        {
            //创建表达式
            var exp = Expressionable.Create<WorkflowExample>();
            exp.AndIF(query.ApproveStatus != null, x => x.ApproveStatus == query.ApproveStatus);
            exp.AndIF(query.WorkflowStatus != null, x => x.WorkflowStatus == query.WorkflowStatus);
            if (query.IsSelfPending)
            {
                var entityIdList = await _workflowService.GetSelfPendingEntityIdList();
                exp.And(x => entityIdList.Contains(x.Id));
            }

            var list = await _rep.GetPageListAsync(exp.ToExpression(), query, x => x.CreateTime, OrderByType.Desc);

            var rlist = list.Adapt<List<WorkflowExampleModel>>();
            await _workflowService.SetApproveInfo(rlist);

            return rlist;
        }

        public async Task<List<ApproveTimeLine>> GetTimeLine(long id)
        {
            var list = await _workflowService.GetApproveTimeLine<WorkflowExample>(id);

            return list;
        }

        public async Task<List<WorkflowEnumModel>> GetApproveStatusSelect()
        {
            return await _workflowService.GetEnumList<ApproveStatusEnum>();
        }

        public async Task<List<WorkflowEnumModel>> GetWorkflowStatusSelect()
        {
            return await _workflowService.GetEnumList<ApproveStatusEnum>();
        }

        public async Task<List<string>> QueryFlowNameSelect()
        {
            return await _workflowService.QueryFlowNameSelect();
        }

        public async Task<bool> Create(WorkflowExampleEdit p)
        {
            var data = new WorkflowExample
            {
                BussinessName = p.BussinessName,
                Days = p.Days
            };
            return await data.InsertAsync();
        }

        public async Task<bool> Update(WorkflowExampleEdit p)
        {
            var entity = WorkflowExample.Find(p.Id);
            if (entity != null)
            {
                p.Adapt(entity);
                return await entity.UpdateAsync();
            }
            return false;
        }

        public async Task<bool> Submit(long id, string flowName)
        {
            var workflow = await _workflowService.StartWorkflowAsync<WorkflowExample>(id, flowName);

            return workflow != null ? true : false;
        }

        public async Task<bool> Cancel(long id)
        {
            return await _workflowService.CancelWorkflowAsync<WorkflowExample>(id);
        }

        public async Task<bool> Delete(long id)
        {
            return await _workflowService.DeleteWorkflowAsync<WorkflowExample>(id);
        }

        public async Task<bool> Approve(BaseApproveInfo approve)
        {
            var workflow = await _workflowService.ContinueWorkflowAsync<WorkflowExample>(approve);

            return workflow != null ? true : false;
        }
    }
}
